package com.ruoyi.ma.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ma.domain.bo.MaMaterialSupplierBo;
import com.ruoyi.ma.domain.vo.MaMaterialSupplierVo;
import com.ruoyi.ma.domain.MaMaterialSupplier;
import com.ruoyi.ma.mapper.MaMaterialSupplierMapper;
import com.ruoyi.ma.service.IMaMaterialSupplierService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 材料供应商信息Service业务层处理
 *
 * @author tll
 * @date 2024-11-18
 */
@RequiredArgsConstructor
@Service
public class MaMaterialSupplierServiceImpl implements IMaMaterialSupplierService {

    private final MaMaterialSupplierMapper baseMapper;

    /**
     * 查询材料供应商信息
     */
    @Override
    public MaMaterialSupplierVo queryById(Long supplierId){
        return baseMapper.getById(supplierId);
    }

    /**
     * 查询材料供应商信息列表
     */
    @Override
    public TableDataInfo<MaMaterialSupplierVo> queryPageList(MaMaterialSupplierBo bo, PageQuery pageQuery) {
        QueryWrapper<MaMaterialSupplier> lqw = buildQueryWrapper(bo);
//        Page<MaMaterialSupplierVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<MaMaterialSupplierVo> result = baseMapper.page(pageQuery.build(), lqw, bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询材料供应商信息列表
     */
    @Override
    public List<MaMaterialSupplierVo> queryList(MaMaterialSupplierBo bo) {
        QueryWrapper<MaMaterialSupplier> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<MaMaterialSupplier> buildQueryWrapper(MaMaterialSupplierBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<MaMaterialSupplier> lqw = new QueryWrapper<>();
        lqw.eq(bo.getProjectId() != null, "a.project_id", bo.getProjectId());
        lqw.eq(bo.getBdId() != null, "a.bd_id", bo.getBdId());
        lqw.eq(bo.getZdId() != null, "a.zd_id", bo.getZdId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), "a.name", bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getGhcl()), "a.ghcl", bo.getGhcl());
        lqw.like(StringUtils.isNotBlank(bo.getGhclDictValue()), "a.ghcl_dict_value", bo.getGhclDictValue());
        lqw.like(StringUtils.isNotBlank(bo.getContacts()), "a.contacts", bo.getContacts());
        lqw.like(StringUtils.isNotBlank(bo.getPhone()), "a.phone", bo.getPhone());
        lqw.like(StringUtils.isNotBlank(bo.getFax()), "a.fax", bo.getFax());
        lqw.like(StringUtils.isNotBlank(bo.getMobile()), "a.mobile", bo.getMobile());
        lqw.like(StringUtils.isNotBlank(bo.getAddress()), "a.address", bo.getAddress());
        lqw.like(StringUtils.isNotBlank(bo.getIntroduction()), "a.introduction", bo.getIntroduction());
        lqw.eq(StringUtils.isNotBlank(bo.getFileId()), "a.file_id", bo.getFileId());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptId()), "a.dept_id", bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getDeptName()), "a.dept_name", bo.getDeptName());
        lqw.like(StringUtils.isNotBlank(bo.getRemark()), "a.remark", bo.getRemark());
        return lqw;
    }

    /**
     * 新增材料供应商信息
     */
    @Override
    public Boolean insertByBo(MaMaterialSupplierBo bo) {
        MaMaterialSupplier add = BeanUtil.toBean(bo, MaMaterialSupplier.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSupplierId(add.getSupplierId());
        }
        return flag;
    }

    /**
     * 修改材料供应商信息
     */
    @Override
    public Boolean updateByBo(MaMaterialSupplierBo bo) {
        MaMaterialSupplier update = BeanUtil.toBean(bo, MaMaterialSupplier.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MaMaterialSupplier entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除材料供应商信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
